package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

public class BaseServletResponseProcessor {
	private static BaseServletResponseProcessor processor = getInstance();

	private BaseServletResponseProcessor() {}

	private static BaseServletResponseProcessor getInstance() {
		return new BaseServletResponseProcessor();
	}

	protected static BaseServletResponseProcessor getDefaultInstance() {
		return processor;
	}

	protected void alertResponse(HttpServletResponse res, String msg) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			
			out.println(
				"<html><script>"
					+ "document.location.href='javascript:history.go(-1)'\n"
					+ "alert('"
					+ msg
					+ "')</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void alertThisReloadResponse(HttpServletResponse res, String msg) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			
			out.println(
				"<html><script>"
					+ "document.location.href='javascript:history.go(-1)'\n"
					+ "alert('"
					+ msg
					+ "');" 
					+ " document.location.reload(); "
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}


	protected void alertLocationHref(HttpServletResponse res, String url, String msg) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			
			out.println(
				"<html><script>"
			        + "alert('"
			        + msg
			        + "');"
					+ "document.location.href='"+url+"';\n"
					+"</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void onlyAlertResponse(HttpServletResponse res, String msg) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			
			out.println(
				"<html><script>"
					+ "alert('"
					+ msg
					+ "')</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void alertPopupCloseResponse(HttpServletResponse res, String msg) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			
			out.println(
				"<html><script>"
					+ "alert('"
					+ msg
					+ "');\n"
					+ "opener.document.location.reload();\n"
					+ "self.close()"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void alertCloseResponse(HttpServletResponse res, String msg) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "alert('"
					+ msg
					+ "');\n"
					+ "self.close()"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void alertResponse(HttpServletResponse res, String url, String msg) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		out = res.getWriter();
		out.write("<form name=\"theForm\" method=\"post\" action=\"" + url + "\">");
		out.write("</form>");
		out.println(
			"<html><script>" + "alert('" + msg + "')\n " + "document.theForm.submit();</script>" + "</html>");
	}
	
	protected void alertResponse(HttpServletResponse res, String url, String msg, String method) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		out = res.getWriter();
		out.write("<form name=\"theForm\" method=\"" +method + "\" action=\"" + url + "\">");
		out.write("</form>");
		out.println(
			"<html><script>" + "alert('" + msg + "')\n " + "document.theForm.submit();</script>" + "</html>");
	}



	protected void alertHistoryBack(HttpServletResponse res, String msg, int backCnt) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		out = res.getWriter();
		out.println(
			"<html><script>" + "alert('" + msg + "')\n " + " history.back("+backCnt+");</script>" + "</html>");
	}

	protected void alertResponseOpenerReload(HttpServletResponse res, String url, String msg) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		out = res.getWriter();
		out.write("<form name=\"theForm\" method=\"post\" action=\"" + url + "\">");
		out.write("</form>");
		out.println(
			"<html><script>" + "alert('" + msg + "');\n" + "opener.document.location.reload();\n</script>" + "</html>");
	}
	
	protected void closePopupResponse(HttpServletResponse res, String url) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "opener.document.location.href='"
					+ url
					+ "';"
					+ "self.close()"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void closePopupResponseWithDomain(HttpServletResponse res, String url) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ " if(document.domain.indexOf(\"dnshop.com\") > -1){ \n" +
				    "   document.domain = 'dnshop.com'; \n" +
				    " } else { " +
				    "    document.domain = 'daum.net'; \n" +
				    " } \n"
					+ "opener.document.location.href='"
					+ url
					+ "';"
					+ "self.close()"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}



	protected void closePopupResponse(HttpServletResponse res) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "self.close()"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}
	
	protected void ThisReloadResponse(HttpServletResponse res, String url) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "document.location.reload()"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void ThisRedirectResponse(HttpServletResponse res, String url) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "document.location.href='"
					+ url
					+ "';"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void CloseAndOpenerReloadResponse(HttpServletResponse res) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "opener.document.location.reload();"
					+ "self.close();"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void OpenerReloadResponse(HttpServletResponse res, String msg) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "alert('" + msg + "');\n "
					+ "opener.document.location.reload();\n"
					+ "self.close();\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void TopReloadResponse(HttpServletResponse res, String msg) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "alert('" + msg + "');\n "
					+ "alert('opener='+opener.document.location);\n"
					+ "alert('top='+top.document.location);\n"
					+ "alert('parents='+parent.document.location);\n"
					+ "top.document.location.reload();\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void ParentReloadResponse(HttpServletResponse res, String msg) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "alert('" + msg + "');\n "
					+ "parent.document.location.reload();\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}


	protected void setParentResponse(HttpServletResponse res, String scriptmsg, String bodymsg) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ scriptmsg + "\n "
					+ "</script>"
					+"<body> " + bodymsg + "</body></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void SelfCloseParentReloadResponse(HttpServletResponse res, String msg) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "alert('" + msg + "');\n "
					+ "parent.document.location.reload();\n"
					+ "self.close();\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void OpenerReloadAndRedirect(HttpServletResponse res, String url) throws IOException{
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				" <html><script> "
					+ " opener.document.location.reload();\n "
					+ " document.location.href='"
					+ url
					+ "'; \n"
					+ " </script></html> ");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	protected void closePopupAndOpenerMathodCallResponse(HttpServletResponse res, String msg, String mathodName) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "alert('" + msg + "');\n "
					+ "opener."+mathodName+"();\n"
					+ "self.close();\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}

	}

	public void closePopupRedirect(HttpServletResponse res, String msg, String url) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "alert('" + msg + "');\n "
					+ " opener.document.location.href='"
					+ url
					+ "'; \n"
					+ "self.close();\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}

	public void openerMathodCallWithParam(HttpServletResponse res, String mathodWithParam) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
			        + "document.domain='daum.net';"
					+ " opener."+mathodWithParam+";\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}

	}

	public void openerMathodCallWithParamNotDomain(HttpServletResponse res, String mathodWithParam) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ " opener."+mathodWithParam+";\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}

	}

	public void openerMathodCallAndClosePopUp(HttpServletResponse res, String mathodWithParam) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>\n" +
				    " if(document.domain.indexOf(\"dnshop.com\") > -1){ \n" +
				    "   document.domain = 'dnshop.com'; \n" +
				    " } else { " +
				    "    document.domain = 'daum.net'; \n" +
				    " } \n" +
			        "self.close();\n" +
					" opener."+mathodWithParam+";\n" +
					"</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}

	}

	public void openerMathodCallAndClosePopUpNotDocument(HttpServletResponse res, String mathodWithParam) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>\n" +
			        "self.close();\n" +
					" opener."+mathodWithParam+";\n" +
					"</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}

	}
	
	protected void onlyOpenerReload(HttpServletResponse res) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "opener.document.location.reload();\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}
	
	protected void onlyOpenerParentReload(HttpServletResponse res) throws IOException {
		PrintWriter out = null;
		res.setContentType("text/html; charset=euc-kr");
		try {
			out = res.getWriter();
			out.println(
				"<html><script>"
					+ "opener.parent.document.location.reload();\n"
					+ "self.close();\n"
					+ "</script></html>");
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (Exception e) {
				}
		}
	}
}
